package com.zls.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zls.pojo.TDevice;
import com.zls.pojo.TDeviceGather;
import com.zls.pojo.TDeviceType;
import com.zls.pojo.TFarm;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

public interface TDeviceGatherMapper extends BaseMapper<TDeviceGather> {

    @Results(id = "gatherMap", value = {
            @Result(column = "id", property = "id"),
            @Result(column = "device_id", property = "deviceId"),
            @Result(column = "device_sn", property = "deviceSn"),
            @Result(column = "device_name", property = "deviceName"),
            @Result(column = "device_type", property = "deviceType"),
            @Result(column = "basic_data", property = "basicData"),
            @Result(column = "measurement_unit_id", property = "measurementUnitId"),
            @Result(column = "measurement_unit_name", property = "measurementUnitName"),
            @Result(column = "measurement_unit_type", property = "measurementUnitType"),
            @Result(column = "gather_time", property = "gatherTime"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "update_time", property = "updateTime"),
            @Result(column = "device_id", property = "tDevice", javaType = TDevice.class, one =
            @One(select = "com.zls.mapper.TDeviceMapper.findById", fetchType = FetchType.EAGER))
    })
    @Select("<script>" +
            "select * from t_device_gather where 1=1" +
            "<if test='queryString!=null'> and device_name like concat( '%',#{queryString},'%')" +
            "order by gather_time desc limit #{currentPage},#{pageSize}</if>" +
            "<if test='queryString==null'>order by gather_time desc limit #{currentPage},#{pageSize} </if> "+
            "</script>")
    List<TDeviceGather> findPage(@Param("currentPage") Integer currentPage, @Param("pageSize") Integer pageSize, @Param("queryString") String queryString);

    //查询总记录数
    @Select("<script>" +
            "select count(*) from t_device_gather where 1 =1" +
            "<if test='queryString!=null'> and device_name like concat( '%',#{queryString},'%') </if>" +
            "</script>")
    Long count(String queryString);

    @Insert("INSERT INTO t_device_gather ( device_id, device_sn, device_name,device_type, basic_data, gather_time, " +
            "create_time, update_time ) VALUES ( #{deviceId}, #{deviceSn}, #{deviceName},#{deviceType}, #{basicData}, " +
            "#{gatherTime}, #{createTime}, #{updateTime} )")
    int insertOne(TDeviceGather tDeviceGather);
}
